package m2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //定义队列
        c.queueDeclare("helloworld", false, false, false, null);
        //回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {

            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg = new String(delivery.getBody());
                System.out.println("收到："+msg);
                //遍历消息字符串的所有字符
                for (int i=0;i<msg.length();i++){
                    if (msg.charAt(i) == '.'){
                        try {
                            Thread.sleep(1000);
                        }catch (InterruptedException e){

                        }
                    }
                }
                System.out.println("消息处理结束========================\n");
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {

            @Override
            public void handle(String s) throws IOException {

            }
        };
        //消费数据
        c.basicConsume("helloworld", true,deliverCallback,cancelCallback);

    }
}
